#include "UserServiceImpl.h"
#include "UserContainer.h"

// 创建用户
::grpc::Status UserServiceImpl::CreateUser(::grpc::ServerContext* context, const ::user_service::CreateUserRequest* request, ::user_service::CreateUserResponse* response)
{
    bool ret= UserContainer::GetInstance()->AddUser(request->id(),request->name(),request->age());
    if(ret)
    {
        std::cout<<"添加用户成功,ID:"<<request->id()<<",姓名:"<<request->name()<<",年龄:"<<request->age()<<std::endl;
        response->set_success(true);
    }
    else
    {
        std::cout<<"添加用户失败,ID已存在/ID不合理"<<std::endl;
        response->set_success(false);
    }
    return ::grpc::Status::OK;
}
// 获取用户
::grpc::Status UserServiceImpl::GetUser(::grpc::ServerContext* context, const ::user_service::GetUserRequest* request, ::user_service::GetUserResponse* response)
{
    auto userinfo = response->mutable_user_info();
    bool ret = UserContainer::GetInstance()->GetUser(request->id(),userinfo);
    if (ret)
    {
        std::cout<<"获取用户成功,ID:"<<request->id()<<",姓名:"<<userinfo->name()<<",年龄:"<<userinfo->age()<<std::endl;
        response->set_success(true);
    }
    else
    {
        std::cout<<"获取用户失败,ID不存在/ID不合理"<<std::endl;
        response->set_success(false);
    }

    return ::grpc::Status::OK;
}
// 更新用户
::grpc::Status UserServiceImpl::UpdateUser(::grpc::ServerContext* context, const ::user_service::UserInfo* request, ::user_service::UpdateUserResponse* response)
{
    bool ret = UserContainer::GetInstance()->UpdateUser(request->id(),request->name(),request->age());
    if(ret)
    {
        std::cout<<"更新用户成功,ID:"<<request->id()<<",姓名:"<<request->name()<<",年龄:"<<request->age()<<std::endl;
        response->set_success(true);
    }
    else
    {
        response->set_success(false);
    }
    return ::grpc::Status::OK;
}
// 删除用户
::grpc::Status UserServiceImpl::DeleteUser(::grpc::ServerContext* context, const ::user_service::DeleteUserRequest* request, ::user_service::DeleteUserResponse* response)
{
    bool ret = UserContainer::GetInstance()->DeleteUser(request->id());
    if(ret)
    {
        std::cout<<"删除用户成功,ID:"<<request->id()<<std::endl;
        response->set_success(true);
    }
    else
    {
        std::cout<<"删除用户失败,ID不存在/ID不合理"<<std::endl;
        response->set_success(false);
    }
    return ::grpc::Status::OK;
}
